package com.sixmac.platform;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.CourseChapter;
import com.sixmac.model.Examin;
import com.sixmac.service.CourseChapterService;
import com.sixmac.service.ExaminService;
import com.sixmac.util.ReadExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * CourseChapterController
 * 2018-07-03 17:19:48
 *
 * @author Blade
 */
@Controller
@RequestMapping("/courseChapter")
public class CourseChapterController extends BaseController {
    private static String CODE = "courseChapter";
    private static String PREFIX = "t_course_chapter";
    private static String LIST_SOURCE = "courseChapter.list";
    private static String BASE_PATH = "/platform/courseChapter/";

    @Autowired
    CourseChapterService service;
    @Autowired
    ExaminService examinService;

    @RequestMapping("/exportExcel/{id}")
    @ResponseBody
    public AjaxResult exportExcel(@PathVariable Integer id) {
        CourseChapter courseChapter = service.findById(id);
        if (courseChapter.getTest() != null) {
            return examinService.exportExcel(Convert.toInt(courseChapter.getTest()));
        } else {
            return error("章节测试不存在");
        }

    }
    @RequestMapping("/importExcel")
    public String importExcel(ModelMap map) {
        return examinService.importExcel(map);
    }


    @RequestMapping("/excel")
    public String excel(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "excel1.html";
    }

    @RequestMapping("/excel/{id}")
    public String excelAddex(ModelMap mm, @PathVariable Integer id) {
        mm.put("code", CODE);
        mm.put("examinId", id);
        return BASE_PATH + "excel.html";
    }

    @RequestMapping("/readExcel")
    @ResponseBody
    public AjaxResult readExcel(@RequestParam(value = "excelFile") MultipartFile file, Integer courseId, Integer type) throws Exception {
        CourseChapter courseChapter = Blade.create(CourseChapter.class).findById(courseId);


        if (file != null) {
            byte[] in = file.getBytes();
            if (courseId == null || type == null) {
                return error("添加失败");
            }
            try {
                Examin examin = new Examin();
                examin.setType(2);
                examin.setTitle(courseChapter.getName());
                Blade.create(Examin.class).saveAndSetKey(examin);
                new ReadExcel().readExcel(file, examin.getId(), 2);
                courseChapter.setTest(Convert.toStr(examin.getId()));
                Blade.create(CourseChapter.class).update(courseChapter);
                return success("添加成功");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return error("添加失败");
        }

        return error("添加失败");
    }

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "courseChapter.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "courseChapter_add.html";
    }


    @RequestMapping(KEY_ADD + "/{id}")
    public String add(ModelMap mm, @PathVariable Integer id) {
        mm.put("code", CODE);
        CourseChapter courseChapter = Blade.create(CourseChapter.class).findById(id);
        if (courseChapter != null) {
            mm.put("courseId", courseChapter.getCourseId());
            mm.put("p", courseChapter.getPid());
        }
        return BASE_PATH + "courseChapter_add.html";
    }


    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        CourseChapter courseChapter = service.findById(id);
        mm.put("model", JsonKit.toJson(courseChapter));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "courseChapter_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        CourseChapter courseChapter = service.findById(id);
        mm.put("model", JsonKit.toJson(courseChapter));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "courseChapter_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Object grid = paginate(LIST_SOURCE);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        CourseChapter courseChapter = mapping(PREFIX, CourseChapter.class);
        courseChapter.setCreatetime(new Date());
        Integer courseId = service.getPid(courseChapter.getPid());
        courseChapter.setCourseId(courseId);
        boolean temp = service.save(courseChapter);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        CourseChapter courseChapter = mapping(PREFIX, CourseChapter.class);
        boolean temp = service.update(courseChapter);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteId(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }
}
